import config from '../config';
import { CellStateEnum } from '../enum';
import { Cell } from '../types';

export type PositionType = {
  x: number;
  y: number;
};

class Position {
  /**
   * getBoardPosition
   */
  public getBoardPosition(matrix: Cell[][]): PositionType[] {
    const collection: PositionType[] = [];
    matrix.forEach((itemY, y) => {
      itemY.forEach((_itemX, x) => {
        collection.push(this.position(x, y));
      });
    });
    return collection;
  }

  /**
   * getActiveCellPosition
   */
  public getActiveCellPosition(matrix: Cell[][]): PositionType[] {
    const collection: PositionType[] = [];
    matrix.forEach((itemY, y) => {
      itemY.forEach((_itemX, x) => {
        if (matrix[y][x] == CellStateEnum.live) {
          collection.push(this.position(x, y));
        }
      });
    });
    return collection;
  }

  public position(x: number, y: number): PositionType {
    return {
      x: x * config.cell.width,
      y: y * config.cell.height,
    };
  }
}

export default new Position();
